{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Function call with callback\n",
    "\n",
    "This is a feature that allows applying some human-in-the-loop concepts in FunctionTool.\n",
    "\n",
    "Basically, a callback function is added that enables the developer to request user input in the middle of an agent interaction, as well as allowing any programmatic action."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-openai\n",
    "%pip install llama-index-agents-openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.tools import FunctionTool\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Function to display to the user the data produced for function calling and request their input to return to the interaction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def callback(message):\n",
    "    confirmation = input(\n",
    "        f\"{message[1]}\\nDo you approve of sending this greeting?\\nInput(Y/N):\"\n",
    "    )\n",
    "\n",
    "    if confirmation.lower() == \"y\":\n",
    "        # Here you can trigger an action such as sending an email, message, api call, etc.\n",
    "        return \"Greeting sent successfully.\"\n",
    "    else:\n",
    "        return (\n",
    "            \"Greeting has not been approved, talk a bit about how to improve\"\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simple function that only requires a recipient and a greeting message."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def send_hello(destination: str, message: str) -> str:\n",
    "    \"\"\"\n",
    "    Say hello with a rhyme\n",
    "    destination: str - Name of recipient\n",
    "    message: str - Greeting message with a rhyme to the recipient's name\n",
    "    \"\"\"\n",
    "\n",
    "    return destination, message\n",
    "\n",
    "\n",
    "hello_tool = FunctionTool.from_defaults(fn=send_hello, callback=callback)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = OpenAI(model=\"gpt-4.1\")\n",
    "agent = FunctionAgent(tools=[hello_tool])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The hello message has been sent to Karen with the rhyme \"Hello Karen, you're a star!\"\n"
     ]
    }
   ],
   "source": [
    "response = await agent.run(\"Send hello to Karen\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have successfully sent a hello message to Joe with the greeting \"Hello Joe, you're a pro!\"\n"
     ]
    }
   ],
   "source": [
    "response = await agent.run(\"Send hello to Joe\")\n",
    "print(str(response))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
